import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, getUserDepID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { contractHomeCountSwitch } from '../mconfig';
import { ILoadMapTypeAll } from '@/common';

const baseUrl = getBaseUrl();

/** 统计预算金额 */
const reportAllByDateStepService = (curRes: Record<string, any>, callback: (res: any) => void) => {
  /** 获取权限信息 */
  const queryTypeObj: Record<string, any> = contractHomeCountSwitch(curRes);

  /** 获取统计数据  */
  const { data, loading, run } = useRequest(
    (payload: Record<string, any>) => {
      const params: Record<string, any> = {
        ...payload,
        // sort: 'capitalDir',
        countAllState: `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1000,${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1001,${getTenantID()}${
          ILoadMapTypeAll.CURRYOUT_STATE
        }1010,${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1004,${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1006,${getTenantID()}${
          ILoadMapTypeAll.CURRYOUT_STATE
        }1014,${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1007,${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1005`,
        searchState: `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1014`,
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/purchaseReportController/reportAllByDateStep/${queryTypeObj?.params}`;
      return requestFunComponFiex('POST', requestURL, params);
    },
    {
      manual: true, //页面初始请求为 true
      onSuccess: callback,
    },
  );

  const reportAllByDateStepRun = (payload: Record<string, any>) => {
    let searchParams: Record<string, any> = {
      ...payload,
    };

    if (searchParams?.timeRanges) {
      const [createTimeStart, createTimeEnd] = searchParams.timeRanges;
      searchParams['startTime'] = createTimeStart.format('YYYY-01-01 00:00:00');
      searchParams['endTime'] = createTimeEnd.format('YYYY-12-31 23:59:59');
      delete searchParams['timeRanges'];
    }

    if (searchParams?.CompanDeptPos) {
      delete searchParams['CompanDeptPos'];
    }
    searchParams['params'] = queryTypeObj?.params;

    run(searchParams);
  };

  return {
    reportAllByDateStepRun: reportAllByDateStepRun,
    reportAllByDateStepLoading: loading,
    reportAllByDateStepInfo: data?.obj,
  };
};

export { reportAllByDateStepService };
/** 统计数量和金额 */
const reportStateByStepService = (curRes: Record<string, any>, callback: (res: any) => void) => {
  /** 获取权限信息 */
  const queryTypeObj: Record<string, any> = contractHomeCountSwitch(curRes);

  /** 获取统计数据  */
  const { data, loading, run } = useRequest(
    (payload: Record<string, any>) => {
      const params: Record<string, any> = {
        ...payload,
        sort: 'seq',
        dicTypeIds: `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}`,
        ...queryTypeObj,
      };
      const requestURL = `${baseUrl}/purchaseReportController/reportStateByStep/${queryTypeObj?.params}`;
      return requestFunComponFiex('POST', requestURL, params);
    },
    {
      manual: true, //页面初始请求为 true
      onSuccess: callback,
    },
  );

  const reportAllByDateStepRun = (payload: Record<string, any>) => {
    let searchParams: Record<string, any> = {
      ...payload,
    };

    if (searchParams?.timeRanges) {
      const [createTimeStart, createTimeEnd] = searchParams.timeRanges;
      searchParams['startTime'] = createTimeStart.format('YYYY-01-01 00:00:00');
      searchParams['endTime'] = createTimeEnd.format('YYYY-12-31 23:59:59');
      delete searchParams['timeRanges'];
    }

    if (searchParams?.CompanDeptPos) {
      delete searchParams['CompanDeptPos'];
    }
    searchParams['params'] = queryTypeObj?.params;

    run(searchParams);
  };

  return {
    reportAllByDateStepRun: reportAllByDateStepRun,
    reportAllByDateStepLoading: loading,
    reportAllByDateStepInfo: data?.obj,
  };
};

export { reportStateByStepService };
